package com.web.controller;

import com.web.bean.课程档案;
import com.web.dao.课程档案Dao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;


@WebServlet("/insertCourse")
public class CourseInsertController extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        req.setCharacterEncoding("UTF-8");

        String 课程代号 = req.getParameter("课程代号");
        String 课程名称 = req.getParameter("课程名称");
        String 类别 = req.getParameter("类别");
        String 考核方式 = req.getParameter("考核方式");
        String 学分 = req.getParameter("学分");
        float s学分 = Integer.parseInt(学分);



        课程档案 cla = new 课程档案(课程代号, 课程名称,类别,考核方式, s学分,null);
        课程档案Dao dao = new 课程档案Dao();
        boolean isok = false;
        try {
            isok = dao.insertClass(cla);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        if (isok) {
            req.getRequestDispatcher("/getAllCourse").forward(req, resp);
        } else {

            req.setAttribute("message", "添加操作失败，请重新操作");
            req.setAttribute("cla", cla);
            req.getRequestDispatcher("/CourseInsert.jsp").forward(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}